package com.xrkj.b2c.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xrkj.b2c.controller.common.BasicController;
import com.xrkj.b2c.dao.sys.SysRolePrivilegeMapper;
import com.xrkj.b2c.model.sys.SysAdmin;
import com.xrkj.b2c.model.sys.SysMember;
import com.xrkj.b2c.model.sys.SysMemberLevel;
import com.xrkj.b2c.model.sys.SysPrivilege;
import com.xrkj.b2c.model.sys.SysRolePrivilege;
import com.xrkj.b2c.service.sys.SysAdminService;
import com.xrkj.b2c.service.sys.SysMemberService;
import com.xrkj.b2c.service.sys.SysPrivilegeService;
import com.xrkj.b2c.service.sys.SysRoleService;
import com.xrkj.b2c.utils.MD5Util;
import com.xrkj.b2c.utils.StringUtil;
import com.xrkj.b2c.utils.UUIDUtil;
import com.xrkj.b2c.view.AdminView;

/**
 * 登录控制器类
 * @author 赵子靖
 *
 */
@RequestMapping("/login")
@Controller
public class XrkjLoginController extends BasicController{
    
    @Autowired
    private SysAdminService adminService;
    @Autowired
    private SysRolePrivilegeMapper rolePrivilegeDao;
    @Autowired
    private SysPrivilegeService privilegeService;
    @Autowired
    private SysRoleService roleService;
    @Autowired
    private SysMemberService memberService;
    
	/**
	 * 转向登录控制器类
	 * @return
	 */
	@RequestMapping("/forwardLogin.do")
	public ModelAndView forwardLogin(HttpServletRequest request,HttpServletResponse response){
		return new ModelAndView("WEB-INF/login");
	}
	
	/**
	 * 
	 * <B>方法名称：</B>跳转手机登录页面<BR>
	 * <B>概要说明：</B><BR>
	 * @author perry
	 * @since 2017年12月11日 	
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/forwardMobileLogin.do")
    public ModelAndView forwardMobileLogin(HttpServletRequest request,HttpServletResponse response){
        return new ModelAndView("WEB-INF/pages/mobile/login");
    }
	
	/**
     * 
     * <B>方法名称：</B>注册<BR>
     * <B>概要说明：</B><BR>
     * @author perry
     * @since 2017年12月12日   
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/forwardMobileReg")
    public ModelAndView forwardMobileReg(HttpServletRequest request, HttpServletResponse response) {
        return new ModelAndView("WEB-INF/pages/mobile/reg");
    }
	
	/**
	 * 用户登录方法
	 * @return
	 */
	@RequestMapping("/login.do")
	@ResponseBody
	public Object login(HttpServletRequest request,HttpServletResponse response){
		String loginName = request.getParameter("username");//账号
		String password = request.getParameter("password");//密码
		String autologin=request.getParameter("autologin");//是否自动登录
		Map<String, Object> ret = new HashMap<String, Object>();
		if(!"".equals(loginName) && null!=loginName && !"".equals(password) && null!=password){
			ret.put("account", loginName);
			//密码进行md5加密
			ret.put("pwd", MD5Util.Md5(password));
			SysAdmin loginAdmin = adminService.findAdminByAccountAndPwd(ret);
			if(loginAdmin!=null){
			    HttpSession session=request.getSession();
			    AdminView adminView = new AdminView();
			    adminView.setId(loginAdmin.getaId());
			    adminView.setAccount(loginAdmin.getaAccount());
			    adminView.setNick(loginAdmin.getaNicheng());
			    adminView.setHeadImg(loginAdmin.getaHeadImg());
			    //设置用户权限 和角色
			    Map<String, Object> privileges = new HashMap<String, Object>();
			    if(!StringUtil.isEmpty(loginAdmin.getaRoleId())){
			        //设置该用户拥有角色
		            adminView.setRole(roleService.selectByPrimaryKey(loginAdmin.getaRoleId()));
			        Map<String, Object> rolePrivilegeMap = new HashMap<String, Object>();
			        rolePrivilegeMap.put("rId", loginAdmin.getaRoleId());
			        List<SysRolePrivilege> rolePrivileges = rolePrivilegeDao.findRolePrivilegeList(rolePrivilegeMap);
			        if(rolePrivileges!=null && rolePrivileges.size()>0){
			            for (SysRolePrivilege rolePrivilege : rolePrivileges) {
			                SysPrivilege privilege = privilegeService.selectByPrimaryKey(rolePrivilege.getpId());
			                if(privilege!=null){
			                    privileges.put(privilege.getpCode(), privilege.getpCode());
			                }
                        }
			        }
			    }
			    //设置该用户拥有权限
			    adminView.setPrivileges(privileges);
			    session.setAttribute("admin",adminView);
                session.setAttribute("userName", loginAdmin.getaAccount());
			    ret.put("status","1");//登录成功
			}else{//账号密码错误
				ret.put("status","-2");
			}
		}else{//请输入账号密码
			ret.put("status","-1");
		}
		return ret;
	}
	
	/**
	 * 
	 * <B>方法名称：</B>移动端登录<BR>
	 * <B>概要说明：</B><BR>
	 * @author perry
	 * @since 2017年12月12日 	
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/doMobileLogin.do")
    @ResponseBody
    public Map<String, Object> doMobileLogin(HttpServletRequest request,HttpServletResponse response){
        Map<String, Object> ret = new HashMap<String, Object>();
	    String account = request.getParameter("account");//账号
        String password = request.getParameter("password");//密码
        if(StringUtil.isEmpty(account)){
            ret.put("success", false);
            ret.put("msg","请输入登录账号");
            return ret;
        }
        if(StringUtil.isEmpty(password)){
            ret.put("success", false);
            ret.put("msg", "请输入密码");
            return ret;
        }
        Map<String, Object> paramter = new HashMap<String, Object>();
        paramter.put("account", account);
        //密码进行md5加密
        paramter.put("pwd", MD5Util.Md5(password));
        SysMember member = memberService.findMemberByAccountAndPwd(paramter);
        if(member==null){
            ret.put("success", false);
            ret.put("msg", "账号或者密码错误。");
            return ret;
        }
        //登录成功
        HttpSession session=request.getSession();
        session.setAttribute("member",member);
        ret.put("success", true);
        ret.put("msg", "登录成功。");
        return ret;
    }
	
	/**
	 * 系统退出
	 * @return
	 */
	@RequestMapping("/exit")
	public ModelAndView exit(HttpServletRequest request,HttpServletResponse response){
		HttpSession session=request.getSession();
		session.removeAttribute("userName");
		return new ModelAndView("WEB-INF/login");
	}
	
	/**
	 * 
	 * <B>方法名称：</B>注册<BR>
	 * <B>概要说明：</B><BR>
	 * @author perry
	 * @since 2017年12月12日 	
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/mobileRegister")
    @ResponseBody
    public Object mobileRegister(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> ret = new HashMap<String, Object>();
	    String mobile = request.getParameter("mobile");
        String password = request.getParameter("password");
        if(StringUtil.isEmpty(mobile)){
            ret.put("success", false);
            ret.put("msg", "请输入账号");
            return ret;
        }
        if(StringUtil.isEmpty(password)){
            ret.put("success", false);
            ret.put("msg", "请输入密码");
            return ret;
        }
        
        SysMember member = new SysMember();
        member.setmId(UUIDUtil.id());
        member.setmAccount(mobile);
        member.setmPwd(MD5Util.Md5(password));
        member.setmMobile(mobile);
        member.setmLevelId(SysMemberLevel.MEMBER_COPPER_LEVEL);
        member.setmNicheng(mobile);
        member.setmNo(memberService.createMemberNo());
        member.setmStatus(SysMember.STATUS_1);
        member.setmCreateTime(new Date());
        if(memberService.insertSelective(member)>0){
            ret.put("success", true);
            ret.put("msg","注册成功。");
            return ret;
        }else{
            ret.put("success", false);
            ret.put("msg", "注册失败。");
            return ret;
        }
    }
	
}
